package com.xiao.asset.controller;

import com.alibaba.fastjson.JSONObject;
import com.xiao.asset.bean.UserBean;
import com.xiao.asset.services.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 衰草寒烟 on 2016/11/2.
 */
@RequestMapping("/user")
@Controller("user")
public class UserController {
    @Autowired
    private UserService userservice;
    public static Logger log=Logger.getLogger(UserController.class);

    /**
     * 登录
     */
    @RequestMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest request, @RequestParam("username") String username, @RequestParam("password") String password){
        String msg="success";
        UserBean result = userservice.login(username, password);
        if(result==null){
            msg="failure";
        }
        JSONObject json=new JSONObject();
        if(StringUtils.equals(msg,"success")){
            json.put("login_status","success");
            json.put("redirect_url","main/home.do");
            request.getSession().setAttribute("USER",result);
        }else {
            json.put("login_status","invalid");
        }
        return json.toJSONString();
    }
}
